﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebUI.Models;
using WebUI.BL.User;
using WebUI.Lang;
namespace WebUI.AdminSite.User
{
    public partial class UserManager : BasePage
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var gridUsers = GetUserGridViewModel(txtFindUser.Text.Trim());
                ShowGridview(gridUsers);
                DisplayLangOnControl();
            }
        }

        private void DisplayLangOnControl()
        {
            btnFind.Text = GetText("user.finduser");
        }

        private string getrole(string name)
        {
            UserBL userbl = new UserBL();
            var x = userbl.getRole(name);
            string s="";
            foreach( BL.ModelView.GroupModelView g in x)
            {
                s+=g.GroupName+" - ";
            }
            if (s.EndsWith(" - "))
            {
                int i=s.LastIndexOf("- ");
                return s.Substring(0, i);
            }
            return null;
        }

        private List<UserGridViewModel> GetUserGridViewModel(string txtFindUser)
        {
            UserBL userbl=new UserBL();
            GroupUserBL group_user_bl=new GroupUserBL();
            GroupBL groupbl=new GroupBL();
            var index = 1;
            if (String.IsNullOrEmpty(txtFindUser))
            {
                
                var objView = (from u in userbl.GetUsers()
                               join gu in group_user_bl.GetGroupUser() on u.UserNo equals gu.UserNo into group1
                               select new UserGridViewModel
                               {
                                   Id = index++,
                                   Name = u.UserLoginName,
                                   Status = u.Status,
                                   Email = u.Email,
                                   Phone = u.Phone,
                                   Roles = getrole(u.Name)
                               }).Distinct();
                             
                return objView.ToList();
            }
            else
            {
                var objUserbl = from u in userbl.GetUsersByName(txtFindUser)
                              join gu in group_user_bl.GetGroupUser() on u.UserNo equals gu.UserNo into group1
                              from g1 in group1.DefaultIfEmpty()
                              join g in groupbl.GetGroup() on new
                              {
                                  GroupNo = (g1 == null ? -1 : g1.GroupNo)
                              } equals new { GroupNo = g.GroupNo } into group2
                              from g2 in group2.DefaultIfEmpty()
                              select new UserGridViewModel
                              {
                                  Id = index++,
                                  Name = u.UserLoginName,
                                  Status = u.Status,
                                  Email = u.Email,
                                  Phone = u.Phone,
                                  Roles = (g2 == null ? String.Empty : g2.GroupName)
                              };
                    return objUserbl.ToList();
            }
        }

        private void ShowGridview(List<UserGridViewModel> gridUsers)
        {
            gridViewUsers.DataSource = gridUsers;
            DataBind();
        }

        protected void btnFind_Click(object sender, EventArgs e)
        {
            var gridUsers = GetUserGridViewModel(txtFindUser.Text.Trim());
            ShowGridview(gridUsers);
        }

        protected void gridViewUsers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[1].Text = GetText("GridHeader.Name");
                e.Row.Cells[2].Text = GetText("GridHeader.Phone");
                e.Row.Cells[3].Text = GetText("GridHeader.Email");
                e.Row.Cells[4].Text = GetText("GridHeader.Status");
                e.Row.Cells[5].Text = GetText("GridHeader.Roles");
                e.Row.Cells[6].Text = GetText("GridHeader.ViewDetails");
            }
        }
    }
}